class TwikitFileReader

	def initialize
		@array_of_files = Hash.new();
	end
	
	def clear_cache()
	  @array_of_files = Hash.new()
	  return true
	end
	
	def read_directory( path, pattern=//, recursion=nil )
	  files_read = Hash.new();
	  if(recursion.nil?)then recursion=-1
	  else if(recursion.is_a? String)then recursion = recursion.to_i end end
	  if(!(path[-1,1]=="/"))then path += "/" end
	  # puts path
	  if(read_dir = Dir.open(path))then
	    read_dir.each{|filename|
	      # puts "Attempting::#{filename}"
	      if((File.directory?( path + filename )) || (filename == ".") || (filename == ".."))then
          # puts "Directory::'#{filename}'" 
          if(filename != "." && filename != "..")then
	         if(recursion > 0)then more_files_read = read_directory( path + filename, pattern, recursion-1)
	         else if(recursion == -1)then more_files_read = read_directory( path + filename, pattern, -1) end end
	         if(!more_files_read.nil? && more_files_read.length > 0)then
	           more_files_read.each_pair{|fp,fc|
	             files_read[fp] = fc
	           }
	         end
	        end
	      else if(filename.index(pattern))then
	        if(this_file = read_files( [path + filename] ))then
	          puts "Matched File::'#{filename}'" 
	          files_read[path + filename] = this_file[path + filename]
	        end
	      else
	        # puts "Unmatched Filename::'#{filename}'"
	      end end
	    }
	  else
	    raise "read_directory::Warning - Could not open '#{path}' "
	  end
	  return @array_of_files = files_read
	end

	def read_files( array_of_filenames )
		if( ( array_of_filenames.length > 0)  ) then
			found_error = false;
			string_filename_count = 0;
			for i in (0 .. array_of_filenames.length-1) do
				if ( !(array_of_filenames[i].kind_of? String ) ) then found_error = true
				else
					string_filename_count += 1
					worked = false;
					begin
						worked = true;
						file = File.open(array_of_filenames[i], "rb")
					rescue Exception
						warn "read_files::Warning - Could not open '#{array_of_filenames[i]}' "
						worked = false;
					end
					if( worked === true ) then
						contents = file.read
						file.close
						# p "Successfully opened '#{array_of_filenames[i]}' :/n #{contents}"
						@array_of_files[ array_of_filenames[i] ] = contents
					end
				end
			end
			if ( @array_of_files.length === 0 ) then
				if(string_filename_count > 0) then warn "read_files::Warning - No files found for provided input values."
				else warn "read_files::Warning - No appropriate filenames provided." end
				return false
			else
				return @array_of_files
			end
		else return false
		end
	end
	
	def get_strings( )
		return @array_of_files
	end
	
	def write_files( hash_of_files )
		if( hash_of_files.length > 0) then
			hash_of_files.each{ |key, val|
				begin
				  if(File::exists?( key ))
  				  file = File.open(key, 'w')
  				else
            h = key
            d = ""
  				  while(h.slice(/^(.+?\/)(.+\..*)$/))do
  				    d = d + $1
  				    h = $2
    				  if(!File::exists?( d ))then
                Dir.mkdir(d)
                warn "Directory '#{d} did not exist but was created."
    				  end
  				  end
            file = File.open(key, 'w')
  				end
				rescue Exception => e
				  warn e.inspect
				end
				file.write(val)
				file.close
			}
		else return false
		end
	end

end